package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.RealFeeQueryResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 11:10
 */
public class RealFeeQueryBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(RealFeeQueryBizSlot.class);
    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String queryFlag = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Query_Flag");
        msgBody.put("queryFlag", queryFlag);
        String billingCycle = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Billing_Cycle");
        msgBody.put("billingCycle", billingCycle);
        String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/DestinationAttr");
        msgBody.put("destinationAttr", destinationAttr);
        String accNbr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id");
        msgBody.put("accNbr", accNbr);

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);



        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

      			LOG.info("request body is {}", msgBodyStr.toString());
            LOG.info("webserviceUrl is {}", url);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
            LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        RealFeeQueryResponse realFeeQueryResponse = new Gson().fromJson(respStr, RealFeeQueryResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", realFeeQueryResponse.getResultCode());
        respDataXml.put("Acct_ID", realFeeQueryResponse.getAcctId());
        respDataXml.put("Acct_Name",realFeeQueryResponse.getAcctName());
        respDataXml.put("Total_Charge",realFeeQueryResponse.getSumCharge());
        respDataXml.put("Charge_Payed",realFeeQueryResponse.getChargePayed());

        List<RealFeeQueryResponse.ItemInformation> itemInformationList = realFeeQueryResponse.getItemInformationList();

        for (RealFeeQueryResponse.ItemInformation query : itemInformationList) {
            TreeMap<String, Object> infoXml = new TreeMap<>();
            infoXml.put("Show_level", query.getShowLevel());
            infoXml.put("Class_Id", query.getClassId());
            infoXml.put("Parent_Class_Id", query.getParentClassId());
            infoXml.put("Charge_type_Name", query.getChargeTypeName());
            infoXml.put("Charge", query.getCharge());
            infoXml.put("serv_id", query.getServId());
            infoXml.put("Destination_Id", query.getDestinationId());
            infoXml.put("SERVICE_TYPE_NAME", query.getServiceTypeName());
            infoXml.put("ChargeShouldPayed", realFeeQueryResponse.getChargeShouldPayed());
            infoXml.put("ProdOfferInstanceId", query.getProdOfferInstanceId());
            infoXml.put("Billing_Cycle", realFeeQueryResponse.getBillingCycleId());
            infoXml.put("Acct_Item_Type_Id",query.getAcctItemTypeId());
            infoXml.put("item_source_flag",query.getItemSourceFlag());
            infoXml.put("Product_Offer_Id",query.getProductOfferId());
            infoXml.put("PaymentFlag",query.getPaymentFlag());
            respDataXml.put("Item_Information", infoXml);
        }
        String respXmlStr="";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
